package leetcode.hot100;

public class Solution169 {

    public static void main(String[] args) {
        int[] elems = new int[]{6,5,5};
        System.out.println(new Solution169().majorityElement(elems));

    }

    public int majorityElement(int[] nums) {
        //投票，两个不同的票相抵消，相同的票计数
        int curMain = nums[0], count = 1, i = 1;
        while(i<nums.length){
            //当前推选者依然占优
            if(curMain==nums[i]) count++;
            //有人来竞争
            else {
                count--;
                if(count==0){ //重新开始推选
                    curMain = nums[++i];
                    count = 1;
                }
            }
            i++;
        }
        return curMain;
    }

}
